// api/house.js
import request from "../request";

export default {
  getHouses(params = {}) {
    // 参数标准化
    const processed = {
      city: params.city || "",
      rentType: params.rentType || "",
      minRent: params.minRent ? Number(params.minRent) : undefined,
      maxRent: params.maxRent ? Number(params.maxRent) : undefined,
      title: params.title || "",
      sortBy: params.sortBy || "createTime",
      order: (params.order || "desc").toLowerCase(),
      page: Math.max(Number(params.page) || 1, 1),
      size: Math.min(Math.max(Number(params.size) || 10, 1), 100),
      id: params.id || undefined,
      status: params.status || undefined,
      ownerId: params.ownerId || undefined,
    };

    // 过滤空值
    const finalParams = Object.fromEntries(
      Object.entries(processed).filter(([_, v]) => v !== undefined && v !== "")
    );

    return request.get("/api/houses/getHouses", { params: finalParams });
  },
  updateStatus(houseId, status) {
    return request.put(`/api/houses/updateStatus/${houseId}/${status}`);
  },
  selectHouseCount() {
    return request.get("/api/houses/selectHouseCount");
  },
  findNewHousesCountByDayThisWeek() {
    return request.get("/api/houses/new-this-week");
  },
  getHousesByStatus(status) {
    return request.get("/api/houses/getHousesByStatus", { params: { status } });
  },
};
